package com.yanqu.road.dataHelper.repair.logic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wpj
 * @Date 2020/8/19
 */
public class GetUnionRankExcel extends BaseRepair {

    public GetUnionRankExcel(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {
        List<Map<String, String>> listMap = new ArrayList<>();
        List<Integer> activityIdList =  StringUtils.stringToIntegerList("9000008,9000085,9000156,9000209,9000374,9000439,9000528,9000625,9000716,9000805,9000896,9000989,9001039,9001055", ",");
        for (int activityId : activityIdList) {
            List<UnionActivityConditionData> list = getRank(activityId);
            int i = 0;
            for (UnionActivityConditionData unionActivityConditionData : list) {
                i++;
                Map<String, String> map = new HashMap<>();
                map.put("activityId", activityId + "");
                map.put("unionUid", unionActivityConditionData.getUnionUid());
                map.put("serverId", getGameServer().getServerInfo().getServerId() + "");
                map.put("rank", i + "");
                map.put("preValue", unionActivityConditionData.getPreValue().toString());
                map.put("value", unionActivityConditionData.getValue().toString());
                map.put("unionInfo", unionActivityConditionData.getUnionBaseInfoJsonStr());
                listMap.add(map);
            }
        }
        DataMgr.addData(listMap);
    }

    private List<UnionActivityConditionData> getRank(int activityId) {
        List<UnionActivityConditionData> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select * from t_u_union_activity_condition where activityId = " + activityId + " order by value+0 desc");
            rs = call.executeQuery();
            while (rs.next()) {
                UnionActivityConditionData data = parseData(rs);
                dataList.add(data);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
            return dataList;
        }
    }


    protected UnionActivityConditionData parseData(ResultSet rs) throws SQLException {
        UnionActivityConditionData data = new UnionActivityConditionData();
        data.setUnionUid(rs.getString("unionUid"));
        data.setActivityId(rs.getInt("activityId"));
        data.setConditionId(rs.getInt("conditionId"));
        String value = rs.getString("value");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setValue(new BigInteger(value));
        }
        value = rs.getString("preValue");
        if(!StringUtils.isNullOrEmpty(value)){
            data.setPreValue(new BigInteger(value));
        }
        data.setGetReward(rs.getBoolean("isGetReward"));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        data.setMemberUserIdList(StringUtils.stringToLongList(rs.getString("memberList"), ","));
        data.setInfo(rs.getString("info"));
        value = rs.getString("unionBaseInfo");
        if(null == value) {
            value = "";
        }
        UnionBaseInfo unionBaseInfo = JSON.parseObject(value, new TypeReference<UnionBaseInfo>() {});
        if(null != unionBaseInfo){
            data.setUnionBaseInfo(unionBaseInfo);
        }
        return data;
    }
}
